package hms.generic;

import hms.master.MasterView;
import hms.permissions.Permission;
import hms.properties.Properties;

import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * <h2>Class Overview</h2> This is an abstract class that should be inherited by
 * all View classes. It handles the basic initialization and control that is
 * associated with all views. Every implementation might not need all the
 * methods provided, but initialize(), init_GUI(), init_Finish(), and display()
 * should be included by every View that is used throughout the program so that
 * all Views follow a standard format for creation and display.
 * 
 * This class will be added on to as the system is developed, but many of the
 * Views will create their own handlers within the implementation that take care
 * of more specific tasks. This abstract class should only handle events that
 * are common to all or multiple Views and leave specific tasks to be
 * implemented by each class that extends this class.
 * 
 * @author Matthew Johnson
 */

public abstract class AbstractGlobalView implements ActionListener {

	// TODO: Add comments here
	static private GUIViewBuilder guiBuilder;

	// TODO: Add comments here
	public void initialize(JPanel panel, MasterView master) {

		guiBuilder = new GUIViewBuilder(panel, master);

	}

	/**
	 * Get the GUIViewBuilder object to access methods directly
	 * 
	 * @return GUIViewBuilder associated with
	 */
	public GUIViewBuilder getGUIFrame() {
		return guiBuilder;
	}

	/**
	 * Should be called by the view that extends this Abstract Class ONLY after
	 * it has been initialized.
	 */
	public void display() {

		// Call the display method for the GUI
		guiBuilder.display();

	}

	public void destroy() {
		guiBuilder.destroy();
	}

	public void update(JPanel i_contentPanel) {
		// Information will not be lost by default
		Properties.informationWillBeLost = false;

		guiBuilder.update(i_contentPanel);
	}

	/**
	 * Sets the warning dialog a user will get about unsaved data.
	 */
	public void informationWillBeLost() {
		Properties.informationWillBeLost = true;
	}

	/**
	 * Clears the warning dialog a user will get about unsaved data.
	 */
	public void informationHasBeenSaved() {
		Properties.informationWillBeLost = false;
	}
	
	/**
	 * Returns true if informationWillBeLost() has been called and
	 * informationHasBeenSaved() has not.
	 * 
	 * @return
	 */
	public boolean isDBOutOfSync() {
		return Properties.informationWillBeLost;
	}

	/**
	 * Return true if permission p is granted to current user.  Return false AND throw dialog if
	 * user does not have permission or is not logged in.
	 * 
	 * @param p
	 * @return
	 */
	protected boolean assertPermissionGranted(Permission p) {
		if (Properties.currentUser != null) {
			if (!Properties.currentUser.isPermissionGranted(p)) {
				JOptionPane
						.showConfirmDialog(null,
								"You do not have the proper permissions for that!\nPermission missing: "
										+ p, "Not Authorized!",
								JOptionPane.DEFAULT_OPTION,
								JOptionPane.WARNING_MESSAGE);
				return false;
			}
		} else {

			JOptionPane.showConfirmDialog(null, "You are not logged in!",
					"Not Authorized!", JOptionPane.DEFAULT_OPTION,
					JOptionPane.WARNING_MESSAGE);
			return false;
		}
		return true;
	}
	
	/**
	 * True if current user has permission p, false otherwise.
	 * 
	 * @param p
	 * @return
	 */
	protected boolean checkPermission(Permission p) {
		return Properties.currentUser.isPermissionGranted(p);
	}

}
